﻿layui.config({
    base: './swuiadmin/swui/' //指定 swui 根路径
    , version: '1.0.0'
}).extend({  //指定js别名
    setter: "config.dev",
    ajax: 'js/ajax',
    layxwin: 'js/layxwin',
    drag: 'js/drag',
    desktop: 'js/swui.desktop',
    taskbar: 'js/swui.taskbar',
    menu: 'js/swui.menu',
    control: 'js/swui.control',
    helper: 'js/swui.helper',
    live2d: 'js/swui.live2d'
}).define(['setter','ajax','layxwin', 'drag', 'desktop', 'taskbar', 'menu', 'control', 'helper', 'live2d'], function (exports) {
    var $ = layui.jquery;

    $(function () {
        if (!swui.isOnline()) {
            layer.alert('未登录或已超时，请重新登录',{icon: 3, title:'提示'}, function(index){
                layer.close(index);
                window.location.href = swui.setter.Page.login;
            });
            return;
        }
        layer.msg('Welcome To WinAdmin', {
            time: 4500,
            offset: '40px',
            btn: ['点击进入全屏'],
            btnAlign: 'c',
            yes: function (index) {
                swui.fullScreen(document.documentElement);
                layer.close(index);
            }
        });
        layxwin.html('layx-com', '演示公告', '<p style="padding:20px;">半成品仅供参观，多数设置本地存储，清除浏览器缓存即失效。<br/><br/>参考winAdmin的模块化开发，各模块与原winAdmin有较大的差别，界面更接近win10，谢谢各位！<br/><br/>特色很多，如：<span style="color:#FF5722">桌面助手，主题设置</span>，大家慢慢参观</p>',
            {width: 400,height: 300,refreshMenu:false,refreshable:false,minMenu:false,minable:false,maxMenu:false,maxable:false,resizable:false});
        var user = layui.data('swui','user');
        swui.config({
            settings: layui.data('swui').settings || {
                color: 32,
                taskbarMode: 'bottom',
                startSize: 'sm',
                bgSrc: 'images/2.jpg',
                lockBgSrc: 'images/bg_04.jpg'
            },  //如果本地配置为空则给默认值
            desktop: {
                options: {} //可以为{}  默认 请求 json/desktopmenu.json
                ,contextmenu: [
                    {name: '打开方式', 
                     childrens: [
                        {name: '新选项卡', handler: function (args){
                            swui.OpenWindow(args[0]);
                        }},
                        {name: '新窗口', handler: function (args){
                            swui.OpenWindow(args[0],1);
                        }}
                     ]},
                    {name: '打开', 
                     handler: function (args) {
                        swui.OpenWindow(args[0]);
                    }},
                    {space: true},
                    {name: '删除', 
                     handler: function (args) {
                        swui.desktop.removeApp(args[0], function(){
                            layer.msg('删除回调');
                        });
                    }},
                    {name: '固定到任务栏', 
                     handler: function (args) {
                        swui.taskbar.addTaskItem($.extend({},args[0],{fix:true}), null, true);
                    }}
                ]
                ,desktopmenu: [
                    {name: '查看', 
                     childrens: [
                        {name: '大图标', handler: function (args){layer.msg('大图标');}},
                        {name: '中等图标', handler: function (args){layer.msg('中等图标');}},
                        {name: '小图标', handler: function (args){layer.msg('小图标');}},
                        {space: true},
                        {name: '其它查看方式', handler: function (args){
                            layer.msg('其它查看方式');
                        }}
                     ]},
                    {name: '排序方式', 
                     handler: function (args) {
                        layer.msg('排序方式');
                    }},
                    {name: '刷新', 
                     handler: function (args) {
                        layxwin.msg('刷新');
                    }},
                    {space: true},
                    {name: '浏览网页', 
                     handler: function (args) {
                        swui.OpenWindow('browser');
                    }}
                ]
                ,events: {
                    dblclick:function (option) {
                        swui.OpenWindow(option);
                    }
                }
            },
            taskbar: {
                options: {} //可以为{}  默认 请求 json/desktopmenu.json
                ,hoverDuring: 500
                ,contextmenu: [//任务项右键菜单
                    {name: '从任务栏取消固定', 
                     handler: function (args) {
                        var code = args[0];
                        var elem = args[1];
                        elem.removeClass('swui-task-fixed');
                        if (elem.children('ul').children().length === 0)
                            swui.taskbar.removeTaskItem({parent: elem});
                    }},
                    {name: '关闭所有窗口', 
                     handler: function (args) {
                        var code = args[0];
                        var elem = args[1];
                        swui.taskbar.removeTaskItem({parent: elem});
                    }},
                    {name: '右键菜单可自定义', 
                     handler: function (args) {
                        layer.msg('自定义回调');
                    }}
                ]
                ,barmenu: [//任务栏空白处右键菜单
                    {setkey:'taskViewHide',//变换内容方式
                     values: [{value:true,name:'显示"任务视图"按钮', 
                     handler: function () {
                        swui.setTaskViewHide(false);
                     }
                    },{value:false,name:'隐藏"任务视图"按钮', 
                     handler: function () {
                        swui.setTaskViewHide(true);
                     }
                    }]
                    },
                    {name: '显示"任务视图"按钮', //打勾方式
                     tickwhen: {key:'taskViewHide', value:false},
                     handler: function () {
                        var v = swui.getSetting('taskViewHide') || false;
                        swui.setTaskViewHide(!v);
                    }},
                    {name: '二分屏', 
                     handler: function () {
                        splitWindows(1,2);
                    }},
                    {name: '四分屏', 
                     handler: function () {
                        splitWindows(2,2);
                    }},
                    {name: '六分屏', 
                     handler: function () {
                        splitWindows(2,3);
                    }},
                    {name: '自定义N分屏', 
                     handler: function () {
                        layxwin.prompt('自定义N分屏',function(){
                                    $(this).children(0).html('<label style="display:inherit;">请选择行列数：</label><div class="layui-form" style="margin-top:10px;"><div style="float:left;"><input type="number" lay-number lay-min="1" lay-max="6" value="1"></div><div style="float:left;line-height:32px;">行</div><div style="float:left;"><input type="number" lay-number lay-min="1" lay-max="6" value="1"></div><div style="float:left;line-height:32px;">列</div></div>');
                                    layui.form.render('number');
                                },function(id,value,textarea, button, event){
                                    var r = $(textarea).find('input[lay-number]:first').val();
                                    var c = $(textarea).find('input[lay-number]:last').val();
                                    splitWindows(r,c);
                        });
                    }},
                    {name: '显示桌面', 
                     handler: function () {
                        swui.elems.taskHide.trigger('click');
                    }},
                    {space: true},
                    {name: '任务栏设置', 
                     handler: function () {
                        swui.OpenWindow('setting', 4);
                    }}
                ]
                ,elemenu: [//任务项窗口预览右键菜单
                    {name: '还原', 
                     handler: function (args) {
                        var id = args[1].attr(swui.winAttr);
                        layxwin.restore(id);
                        args[2].call();
                    }},
                    {name: '最小化', 
                     disablewhen: {key:'status',value:'min'},
                     handler: function (args) {
                        var id = args[1].attr(swui.winAttr);
                        layxwin.min(id);
                        args[2].call();
                    }},
                    {name: '最大化', 
                     handler: function (args) {
                        var id = args[1].attr(swui.winAttr);
                        layxwin.max(id);
                        args[2].call();
                    }},
                    {name: '关闭', 
                     handler: function (args) {
                        var id = args[1].attr(swui.winAttr);
                        layxwin.destroy(id);
                    }}
                ]
                ,events: {//任务项点击等其它事件
                    click:function (option, e) {
                        if (!$(this).hasClass('swui-task-on')) {//未打开任务项，打开新窗口
                            swui.OpenWindow(option);
                        }
                        else {
                            if (option.taskType === 'icon') {
                                $(this).attr('ch','true');
                            }
                            else {
                                var item = $(this).children('ul').children('li');
                                if (item.length === 1) {
                                    $(this).children('ul').hide();
                                    item.attr('ch','true');
                                    item[0].click();
                                    item.removeAttr('ch');
                                    swui.setFocusWindow($(item[0]).attr(swui.winAttr));
                                }
                                else {
                                    var target = e.target || e.srcElement;
                                    if ($(target).is('ul')) return;//点击预览图的滚动条，什么都不改变
                                    else if ($(this).hasClass('swui-taskitems-on'))//解除预览层锁定状态
                                        $(this).removeClass('swui-taskitems-on').children('ul').hide();
                                    else if ($(target).closest('li').is(this))
                                        $(this).addClass('swui-taskitems-on').children('ul').show();//点击后预览层锁定状态，移动鼠标不隐藏预览层
                                    //$(this).addClass(THIS).siblings().removeClass(THIS);
                                    else if ($(target).closest('li').parent().parent().is(this))
                                        $(target).closest('li').parent().hide();
                                }
                            }
                        }
                    },
                    mouseenter:function (obj) {
                        var id = $(obj).attr(swui.winAttr);
                        layxwin.preview(id);
                    },
                    mouseleave:function (obj) {
                        layxwin.endPreview();
                    },
                    mouseup:function (obj) {
                        layxwin.endPreview();
                    }
                }
            },
            menu: {
                options: {}
                ,buttons: [
                    {name: '个人中心', 
                     icon: user.ImagePath,
                     handler: function () {
                        swui.hideStartMenu();
                        swui.OpenWindow($.extend({},swui.menu.menus[user.InfoMenu],{anim:null}));//无动画打开窗口
                    }},
                    {name: '主题设置', 
                     icon: '<fa>fa-cog',
                     handler: function () {
                        swui.hideStartMenu();
                        swui.OpenWindow('setting',0);
                    }},
                    {name: '注销登录', 
                     icon: '<fa>fa-power-off',
                     handler: function () {
                        swui.hideStartMenu();
                        layxwin.confirm('提示','确认注销吗？',function(id){
                            layer.msg('执行注销操作，返回登录界面！');
                            layx.destroy(id);
                            swui.logout();
                        });
                    }}
                ]
                ,contextmenu: [
                    {name: '设置', 
                     icon: '<fa>fa-cog',
                     handler: function (args) {
                        var option = args[0];
                        layer.msg('设置回调');
                    }},
                    {name: '关闭', 
                     icon: '<fa>fa-close',
                     handler: function (args) {
                        var option = args[0];
                        layer.msg('关闭回调');
                    }},
                    {name: '右键菜单可自定义', 
                     icon: '<fa>fa-qq',
                     handler: function (args) {
                        var option = args[0];
                        layer.msg('自定义回调');
                    }}
                ]
                ,tilemenu: [
                    {name: '磁贴设置', 
                     icon: '<fa>fa-cog',
                     handler: function (args) {
                        var option = args[0];
                        layer.msg('磁贴设置回调');
                    }},
                    {name: '取消磁贴', 
                     icon: '<fa>fa-close',
                     handler: function (args) {
                        var option = args[0];
                        layer.msg('取消回调');
                    }},
                    {name: '右键菜单可自定义', 
                     icon: '<fa>fa-qq',
                     handler: function (args) {
                        var option = args[0];
                        layer.msg('磁贴自定义回调');
                    }}
                ]
                ,events: {
                    click:function (option) {
                        swui.hideStartMenu();
                        swui.OpenWindow(option);
                    }
                }
            },
            control: {
                options: {}
                ,events: {
                    click:function (option) {
                        swui.OpenWindow(option);
                    }
                }
            },
            helper: {
                options: {}
                ,events: {
                    note: {
                        name: '便签', 
                        icon: '<fa>fa-pencil-square-o',
                        handler: function () {
                            swui.helper.showMessage({message:'这是便签功能，您可以选择以下操作：'
                                                    ,buttons:[{name:'所有便签',hide:true,handler:function(){
                                                                layer.msg('11');
                                                            }},{name:'新增便签',handler:function(){
                                                                layer.msg('22');
                                                            }}]
                                                    });
                        }
                    },
                    theme: {
                        name: '主题设置',
                        icon: '<fa>fa-paw',
                        handler: function (){
                            swui.OpenWindow('setting',0);
                        }
                    },
                    paper: {
                        name: '切换壁纸',
                        icon: '<fa>fa-television',
                        handler: function () {
                            swui.helper.showMessage('这是便签功能，您可以选择以下操作：显示所有便签  |  新增便签');
                        }
                    },
                    lock: {
                        name: '锁屏',
                        icon: '<fa>fa-power-off',
                        handler: function () {
                            swui.lockScreen();
                        }
                    },
                    ppt: {
                        name: '指引帮助',
                        icon: '<layui>layui-icon-help',
                        handler: function () {
                            swui.helper.guideMode([{elem:'.swui-desktop-item[data-code="theme"]',tip:'这是主题',event:'dblclick'}
                                    ,{elem:'.swui-desktop-item[data-code="auth"]',tip:'这是作者',prev:true}
                                    ,{elem:'.swui-desktop-item[data-code="remark"]',tip:'这是基本说明',prev:true,event:'dblclick'}
                                    ,{elem:'.swui-desktop-item[data-code="sysmenu"]',tip:'这是系统菜单',prev:true,exit:true}
                                    ,{elem:'.swui-desktop-item[data-code="role"]',tip:'这是角色管理',prev:true,exit:true,event:'dblclick'}
                                    ,{elem:'.swui-desktop',tip:'这是桌面',prev:true,exit:true}
                                    ,{elem:'.swui-taskbar-start',tip:'这是菜单列表',prev:true,exit:true}
                                    ,{elem:'.swui-taskbar-desktop',tip:'这是显示隐藏桌面',prev:true,exit:true}
                                    ,{elem:'.swui-taskbar-helper',tip:'我在这里，点击就可以隐藏或显示我',prev:true,exit:true}]);
                        }
                    },
                    exit: {
                        name: '关闭小助手',
                        icon: '<fa>fa-sign-out',
                        handler: function () {
                            swui.helper.close();
                        }
                    }
                }
            },
            live2d: {

            }
        }).init({
            audioPlay: false, //是否播放音乐（开机音乐只会播放一次，第二次播放需要关闭当前页面从新打开，刷新无用）
            renderBg: true //是否渲染背景图 （由于js是写在页面底部，所以不太推荐使用这个来渲染，背景图应写在css或者页面头部的时候就开始加载）
        }, function () {
            //初始化完毕回调
            swui.resetEvent('click','img,.bottom div:first', function(){
                swui.OpenWindow(swui.menu.menus[user.InfoMenu]);
            }, swui.elems.userMenu);
            swui.resetEvent('click','.bottom div:last', function(){
                layxwin.confirm('提示','确认注销吗？',function(id){
                    layer.msg('执行注销操作，返回登录界面！');
                    layx.destroy(id);
                    swui.logout();
                });
            }, swui.elems.userMenu);
            swui.setMessageCount();
        });
        layxwin.setStyle('.layx-window{border-color:var(--win-border,#3baced)!important;}.layx-control-bar {background-color:var(--win-color,var(--taskbar-color)) !important;color:var(--win-font, #fff);} .layx-inlay-menus .layx-icon:hover {background-color: var(--win-hover);}');
    });

    function splitWindows(row,col) {
        var list = layxwin.getFocusWindowList(['prompt','load','msg','alert','confirm']);
        if (list.length === 0) {
            layer.msg('分屏只针对非最小化的窗口，当前无可分屏窗口');
            return;
        }
        var w = swui.elems.desktop.width();
        var h = swui.elems.desktop.height();
        if (h/row<200) layer.msg('窗口最小高度为200px，分屏后部分窗口会被遮挡');
        if (w/col<200) layer.msg('窗口最小宽度为200px，分屏后部分窗口会被遮挡');
        for (var i=0;i<row*col && i<list.length;i++) {
            $(list[i].layxWindow).css({left:(i%col)*w/col,top:Math.floor(i/col)*h/row,width:w/col,height:h/row});
        }
    }

    // 判断是否显示锁屏（这个要放在最后执行）
    if (layui.data('swui',"lockscreen") === true) {
        swui.lockScreen();
    }

    exports('index', {});
});